﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using lakethaiClassLibrary;

public partial class cms_users : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            this.lblMsg.Text = HttpContext.Current.Session["msg"].ToString();
        }
        catch {}
        this.BindingData();
    }
    private void BindingData()
    {
        string roleName = Request.QueryString["role"];
        if (string.IsNullOrEmpty(roleName)) return;
        try
        {
            SqlParameter[] para = { new SqlParameter("roleName", roleName) };
            DataTable dt = myDataBinding.GetData("GetUserInfoInRole", para);
            this.gvUsers.DataSource = dt;
            this.gvUsers.DataBind();
            lblTitle.Text = roleName.ToUpper();
        }
        catch (Exception e)
        {
            string s = e.Message;
        }
    }
    protected void ibtDelete_Click(object sender, ImageClickEventArgs e)
    {
        string userName = string.Empty;
        ImageButton theBut = (ImageButton)sender;
        for (int i = 0; i < gvUsers.Rows.Count; i++)
        {
            ImageButton curBut = (ImageButton)gvUsers.Rows[i].FindControl("ibtDelete");
            if (theBut.UniqueID.CompareTo(curBut.UniqueID) == 0)
            {
                Label lbl = (Label)gvUsers.Rows[i].FindControl("lblUserName");
                try
                {
                    userName = lbl.Text;
                }
                catch {}
                break;
            }
        }
        if (string.IsNullOrEmpty(userName))
        {
            HttpContext.Current.Session["msg"] = "username null";
            return;
        }
        //Membership.DeleteUser(userName);
        Roles.RemoveUserFromRole(userName, Request.QueryString["role"]);
        HttpContext.Current.Session["msg"] = "remove '" + userName + "' from '" + Request.QueryString["role"] + "'";
        Response.Redirect("users.aspx?role=" + Request.QueryString["role"]);
    }
    protected void btnAddUserToRole_Click(object sender, EventArgs e)
    {
        try
        {
            Roles.AddUserToRole(drlUsers.Text, drlRoles.Text);
            Response.Redirect("users.aspx?role=" + drlRoles.Text);
        }
        catch (Exception ex) 
        {
            //string s = ex.Message;
            HttpContext.Current.Session["msg"] = ex.Message;
        }
    }
}
